package com.mytijian.openapi.adapter.alihalt.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.mytijian.openapi.adapter.alihalt.consts.AlihaltConsts;
import com.mytijian.openapi.adapter.alihalt.dao.McodeOrderRelDAO;
import com.mytijian.openapi.adapter.alihalt.exception.AlihaltException;
import com.mytijian.openapi.adapter.alihalt.param.AlihaltOrderQueryParam;
import com.mytijian.openapi.adapter.alihalt.result.AlihaltOrderStatusInfo;
import com.mytijian.openapi.adapter.alihalt.result.AlihaltOrderStatusResult;
import com.mytijian.openapi.adapter.alihalt.service.AlihaltOrderStatusQueryService;
import com.mytijian.openapi.core.client.IOpenAPIClient;
import com.mytijian.openapi.core.client.result.OrderInfo;
import com.mytijian.openapi.core.client.result.OrderListResult;
import com.mytijian.openapi.core.model.AppConfig;
import com.mytijian.openapi.core.service.OpenApiAppConfigService;

@Service
public class AlihaltOrderStatusQueryServiceImpl implements AlihaltOrderStatusQueryService {
	
	private Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	private McodeOrderRelDAO mcodeOrderRelDAO;

	@Autowired
	private OpenApiAppConfigService openAPIService;
	
	@Autowired
	private IOpenAPIClient client;
	
	@Override
	public AlihaltOrderStatusResult getOrderStatus(AlihaltOrderQueryParam param) {
		log.info("[阿里健康] [订单查询] 查询参数:{}", JSON.toJSONString(param));
		
		String mcode = param.getServiceNumber();
		
		String orderNum = mcodeOrderRelDAO.getOrderNumByMcode(mcode);
		
		AlihaltOrderStatusResult result = new AlihaltOrderStatusResult();
		AlihaltOrderStatusInfo info = new AlihaltOrderStatusInfo();
		result.setResponseCode("200");
		result.setResult(info);
		
		if (StringUtils.isEmpty(orderNum)) {
			info.setCheckStateId(0);
			return result;
		}
		
		AppConfig config = openAPIService.getConfig(AlihaltConsts.AppName);
		OrderListResult orderListResult = client.getOrderStatus(config, orderNum);
		
		if (orderListResult == null || StringUtils.isNotEmpty(orderListResult.getErrCode())) {
			log.warn("*** [阿里健康] [订单查询] 订单查询失败 查询参数:{} 开放平台返回:{}", JSON.toJSONString(param), JSON.toJSONString(orderListResult));
			throw new AlihaltException("500", "订单查询失败");
		}

		List<OrderInfo> orderInfoList = orderListResult.getOrders();
		if (orderInfoList == null || orderInfoList.isEmpty()) {
			info.setCheckStateId(0);
			return result;
		}
		OrderInfo order = orderInfoList.get(0);
		
		info.setCheckDate(order.getExamDate());
		
		// 	状态 0:未预约 1:已预约 2:已到检 3:已出报告
		int state = 0;
		if ("success".equals(order.getOrderStatus()) || "unexamined".equals(order.getOrderStatus())
				|| "".equals(order.getOrderStatus())) {
			state=1;
		}
		if ("examined".equals(order.getOrderStatus()) || "finish".equals(order.getOrderStatus())) {
			state=2;
		}
		
		// XXX 3 已出报告，与体检报告一起做
		info.setCheckStateId(state);
		log.info("[阿里健康] [订单查询] 查询成功。返回参数:{}", JSON.toJSONString(result));
		return result;
	}

}
